package com.example.order.controller;

import com.example.order.Order;
import com.example.order.config.DbConfig;
import com.example.order.config.OrderProperties;
import com.example.order.service.OrderService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


//@RefreshScope
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderProperties orderProperties;

    @Resource
    private DbConfig dbConfig;

    @Resource
    private HttpServletRequest request;

//    @Value("${order.timeout}")
//    private String orderTimeout;
//
//    @Value("${order.auto-confirm}")
//    private String orderAutoConfirm;

    @GetMapping("/config")
    public String config() {
        return "order.timeout:" + orderProperties.getTimeout()
                + ",order.auto-confirm:" + orderProperties.getAutoConfirm()
                + ",db.username:" + dbConfig.getUsername()
                + ",db.password:" + dbConfig.getPassword();
    }

    @GetMapping("/create")
    public Order createOrder(@RequestParam("userId") Long userId,
                             @RequestParam("productId") Long productId) {
        String authUserId = request.getHeader("userId");
        log.info("认证会员ID: {}", authUserId);
        return orderService.createOrder(productId, userId);
    }
}
